package cn.exrick.xboot.shop.controller;

import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.shop.entity.ShopArticleLike;
import cn.exrick.xboot.shop.service.IShopArticleLikeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
* <p>
    * 用户点赞  前端控制器
    * </p>
*
* @author yuhaifei
* @since 2022-10-28
*/
@RestController
@RequestMapping("/xboot/shoparticlelike")
public class ShopArticleLikeController {

    @Autowired
    private IShopArticleLikeService articleLikeService;

    @Autowired
    private SecurityUtil securityUtil;

    /**
     * 点击喜欢
     * @param articleLike
     * @return
     */
    @PostMapping("addArticleLike")
    public Result addArticleLike(ShopArticleLike articleLike){
        articleLike.setUserId(securityUtil.getCurrMember().getId());
        articleLike.setCreateTime(new Date());
        articleLike.setUpdateTime(new Date());
        articleLikeService.save(articleLike);
        return ResultUtil.data(articleLike);
    }

    /**
     * 取消喜欢
     * @param tid
     * @return
     */
    @PostMapping("removeArticleLike")
    public Result removeArticleLike(String tid){
        articleLikeService.remove(new QueryWrapper<ShopArticleLike>()
                .eq("tid", tid)
                .eq("user_id", securityUtil.getCurrMember().getId()));
        return ResultUtil.data("取消成功");
    }

}
